home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / makecode.lqr / makecode.lbr / MAKE.C next >
Encoding:
Text File  |  2011-01-29  |  6.3 KB  |  395 lines

  1.  
  2.  
  3. /***** acf4:net.micro.pc / watrose!jmsellens /  5:11 am  Apr  7, 1984*/
  4.  
  5. Subject: PATH Environment variable finder for IBM PC
  6.  
  7.  
  8.  
  9. X - bug killer
  10.  
  11.  
  12.  
  13. These routines are used by 'Find' and 'Make' (see adjacent
  14.  
  15. articles.  Hope they're useful.  -  John
  16.  
  17. ----------------------------------------------------------
  18.  
  19. /* The following are two routines to make it easier to access the
  20.  
  21.    PATH environment variable.  They are written for use with the
  22.  
  23.    DeSmet C Compiler.  Known portability problems:
  24.  
  25.                 - assembler code embedded in the C code
  26.  
  27.                 - requires that code segment register (CS) be set to the
  28.  
  29.                   same value as at program invocation so that the
  30.  
  31.                   program segment prefix can be located.  This would
  32.  
  33.                   probably only be a problem if a "large model"
  34.  
  35.                   compiler is used.
  36.  
  37.         The code is a little kludgy in places but...
  38.  
  39.  
  40.  
  41.         Any suggestions for improvements gratefully accepted.
  42.  
  43. */
  44.  
  45.  
  46.  
  47.  
  48.  
  49. /*
  50.  
  51. Written by John M Sellens, April, 1984
  52.  
  53.  
  54.  
  55. Code is all original except where indicated otherwise.
  56.  
  57.  
  58.  
  59. Until August, 1984:
  60.  
  61.         jmsellens@watrose.UUCP
  62.  
  63.  
  64.  
  65.         107 - 180 Brybeck Cres.
  66.  
  67.         Kitchener, Ontario
  68.  
  69.         N2M 5G4
  70.  
  71.  
  72.  
  73. After August, 1984:
  74.  
  75.         c/o 1135 Lansdowne Ave. SW
  76.  
  77.         Calgary, Alberta
  78.  
  79.         T2S 1A4
  80.  
  81.  
  82.  
  83. (c) Copyright 1984 John M Sellens
  84.  
  85. Permission is granted to use, distribute and/or modify this code unless
  86.  
  87. done for direct commercial profit.  If you find these routines useful,
  88.  
  89. modest contributions (monetary or otherwise) will be gratefully accepted.
  90.  
  91. Author's name, address and this notice must be included in any copies.
  92.  
  93.  
  94.  
  95. */
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. #include <stdio.h>
  104.  
  105.  
  106.  
  107. typedef char *char_ptr;
  108.  
  109.  
  110.  
  111. main()
  112.  
  113. {
  114.  
  115.         /* Example of how to use parse_path() and get_path() */
  116.  
  117.  
  118.  
  119.         int i;
  120.  
  121.         char *get_path();
  122.  
  123.         char_ptr *paths, *parse_path();
  124.  
  125.  
  126.  
  127.         paths = parse_path(get_path());
  128.  
  129.         if (paths != NULL)
  130.  
  131.                 for (i=0; paths[i]!=NULL; i++)
  132.  
  133.                         printf("%s\n",paths[i]);
  134.  
  135.         else
  136.  
  137.                 printf("No path is set\n");
  138.  
  139. }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147. char_ptr *parse_path(ps)
  148.  
  149. char *ps;
  150.  
  151. {
  152.  
  153.         /* Takes the path string returned by get_path and destroys it,
  154.  
  155.            by inserting NULL's and returning an array of pointers to
  156.  
  157.            characters that point to locations in the original path
  158.  
  159.            string.  The pointer after the last pointer into the path
  160.  
  161.            string is NULL.  If the path string is NULL, then NULL is
  162.  
  163.            returned.
  164.  
  165.         */
  166.  
  167.  
  168.  
  169.         int i, j, num;
  170.  
  171.         char_ptr *pa;
  172.  
  173.  
  174.  
  175.         if (ps[0] == '\0')      /* no path */
  176.  
  177.                 return(NULL);
  178.  
  179.  
  180.  
  181.         /* count the number of semi-colons, add 1 = number of paths */
  182.  
  183.         for (num=1, i=0; ps[i] != '\0'; i++)
  184.  
  185.                 if (ps[i] == ';')
  186.  
  187.                         num++;
  188.  
  189.  
  190.  
  191.         pa = (char_ptr *)malloc(sizeof(char_ptr) * (num+1));
  192.  
  193.         pa[num] = NULL;
  194.  
  195.  
  196.  
  197.         /* now loop through and point to each path */
  198.  
  199.         for (i=j=0; i<num; i++) {
  200.  
  201.                 pa[i] = &ps[j];
  202.  
  203.                 while(ps[j]!=';' && ps[j]!='\0')
  204.  
  205.                         j++;
  206.  
  207.                 ps[j++] = '\0';
  208.  
  209.         }
  210.  
  211.  
  212.  
  213.         return(pa);
  214.  
  215.  
  216.  
  217. }
  218.  
  219.  
  220.  
  221.  
  222.  
  223. char *get_path()
  224.  
  225. {
  226.  
  227.         /* Returns a pointer to a string containing the value of
  228.  
  229.            the environment variable PATH.  E.g. if PATH=A:\;B:\
  230.  
  231.            the string "A:\;B:\" is returned.  If no PATH is set,
  232.  
  233.            an empty string is returned.
  234.  
  235.         */
  236.  
  237.  
  238.  
  239.         static int path_seg;
  240.  
  241.         /* these three declarations must be first so that they can be
  242.  
  243.            referred to by stack address near the end when copying
  244.  
  245.            the PATH variable */
  246.  
  247.         char *base, *path;
  248.  
  249.         int siz;
  250.  
  251.  
  252.  
  253.         char *next;
  254.  
  255.         char env_str[10];       /* leave a little extra space just in case */
  256.  
  257.         int i, done;
  258.  
  259.  
  260.  
  261. #asm
  262.  
  263. ;               determine the segment that the environment starts at
  264.  
  265. ;               this requires that cs is set to the same value as at
  266.  
  267. ;               program entry i.e. 100H past the program segment prefix
  268.  
  269.                 push es
  270.  
  271.                 mov ax,cs
  272.  
  273.                 sbb ax,10h
  274.  
  275.                 mov es,ax
  276.  
  277.                 mov ax,es:[2cH]
  278.  
  279.                 pop es
  280.  
  281.                 mov word get_path_path_seg_,ax
  282.  
  283. #
  284.  
  285.  
  286.  
  287.         /* look for the start of PATH= */
  288.  
  289.         /* there's always going to be at least one thing in environment */
  290.  
  291.         done = FALSE;
  292.  
  293.         next = 0;
  294.  
  295.         env_str[0] = '\0';
  296.  
  297.         while ((strncmp("PATH=",env_str,5) != 0) && !done) {
  298.  
  299.                 base = next;
  300.  
  301.                 /* set DS to path_seg */
  302.  
  303. #asm
  304.  
  305.                 push ds
  306.  
  307.                 mov ds,word get_path_path_seg_
  308.  
  309. #
  310.  
  311.                 if (base[0] == '\0')
  312.  
  313.                         done = TRUE;    /* no more variables to look at */
  314.  
  315.                 else {
  316.  
  317.                         /* now copy 5 characters into memory */
  318.  
  319.                         for (i=0; i<5; i++)
  320.  
  321.                                 env_str[i] = base[i];   /* ok since env_str is on stack */
  322.  
  323.                         /* now set next to first character after end of this string */
  324.  
  325.                         while ((next++)[0] != '\0')
  326.  
  327.                                 ;
  328.  
  329.                 }
  330.  
  331.                 /* reset DS */
  332.  
  333. #asm
  334.  
  335.                 pop ds
  336.  
  337. #
  338.  
  339.         }
  340.  
  341.         if (done) {     /* no PATH environment variable set */
  342.  
  343.                 path = (char *)malloc(1);
  344.  
  345.                 path[0] = '\0';
  346.  
  347.         } else {
  348.  
  349.                 siz = next - base - 5;
  350.  
  351.                 path = (char *)malloc(siz);
  352.  
  353.                 base += 5;      /* skip over 'PATH=' */
  354.  
  355.                 /* copy the PATH string to path */
  356.  
  357. #asm
  358.  
  359.                 push cx
  360.  
  361.                 push es
  362.  
  363.                 push ds
  364.  
  365.                 mov cx,ds
  366.  
  367.                 mov es,cx
  368.  
  369.                 mov ds,word get_path_path_seg_
  370.  
  371.                 mov si,word [bp-2]      ;base
  372.  
  373.                 mov di,word [bp-4]      ;path
  374.  
  375.                 mov cx,word [bp-6]      ;siz
  376.  
  377.                 rep movsb
  378.  
  379.                 pop ds
  380.  
  381.                 pop es
  382.  
  383.                 pop cx
  384.  
  385. #
  386.  
  387.         }
  388.  
  389.         return(path);
  390.  
  391. }
  392.  
  393. /* ---------- */
  394.  
  395. %